﻿Imports System.Linq
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Columns


Public Class fManutencaoProjectos
    Public ano As Integer
    Public db As New lqProjectosDataContext

    Private Sub fManutencaoProjectos_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ListaAno = (From c In db.tblProjectos Group By c.ano Into Group _
                      Select Ano)

        For Each anoins In ListaAno
            ComboBoxEdit1.Properties.Items.Add(anoins.ToString)
        Next
        ComboBoxEdit1.SelectedIndex = ListaAno.Count

        TblProjectosTableAdapter.Fill(DsProjectos.tblProjectos, ComboBoxEdit1.EditValue, GlobalVariables.Empresa, GlobalVariables.Un)

        GridView1.BestFitColumns()
    End Sub


    Private Sub ComboBoxEdit1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxEdit1.SelectedIndexChanged
       
        If ComboBoxEdit1.SelectedItem <> "" Then
            ano = ComboBoxEdit1.SelectedItem.ToString

            TblProjectosTableAdapter.Fill(DsProjectos.tblProjectos, ano, GlobalVariables.Empresa, GlobalVariables.Un)
        End If
    End Sub

    Private Sub GridView1_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles GridView1.CellValueChanged
        
        Dim view As GridView = TryCast(sender, GridView)
        If view.IsNewItemRow(e.RowHandle) Then

        Else
            If e.Column.Name = colCode.Name Then
                MessageBox.Show("Código não pode ser alterado.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
                TblProjectosTableAdapter.Fill(DsProjectos.tblProjectos, ComboBoxEdit1.EditValue, GlobalVariables.Empresa, GlobalVariables.Un)

            End If

            If e.Column.Name = colDescricao.Name Then
                Dim Desc = (From c In db.tblProjectos _
                Where c.Code = GridView1.GetFocusedRowCellValue(colCode).ToString AndAlso c.ano = ComboBoxEdit1.Text AndAlso c.Company = GlobalVariables.Empresa _
                Select c).SingleOrDefault

                With Desc
                    .Descricao = GridView1.GetFocusedRowCellValue(colDescricao)
                End With

                db.SubmitChanges()

            End If

            If e.Column.Name = colun.Name Then

                Dim un = (From c In db.tblProjectos _
                Where c.Code = GridView1.GetFocusedRowCellValue(colCode).ToString AndAlso c.ano = ComboBoxEdit1.Text AndAlso c.Company = GlobalVariables.Empresa _
                Select c).SingleOrDefault

                With un
                    .un = GridView1.GetFocusedRowCellValue(colun).ToString
                End With

                db.SubmitChanges()

            End If

        End If
    End Sub

    Private Sub GridView1_ValidateRow(sender As Object, e As DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs) Handles GridView1.ValidateRow
        Dim view As GridView = TryCast(sender, GridView)
        If view.IsNewItemRow(e.RowHandle) Then
            Dim columnUN As GridColumn = view.Columns("un")
            Dim valUN As Object = view.GetRowCellValue(e.RowHandle, columnUN)
            If valUN Is Nothing OrElse valUN.ToString() = String.Empty Then
                e.Valid = False
                e.ErrorText = "Gestor é obrigatório."
                view.SetColumnError(columnUN, "Campo não pode estar Vazio")
            End If
            Dim columnDesc As GridColumn = view.Columns("Descricao")
            Dim valDesc As Object = view.GetRowCellValue(e.RowHandle, columnDesc)
            If valDesc Is Nothing OrElse valDesc.ToString() = String.Empty Then
                e.Valid = False
                e.ErrorText = "Descrição é obrigatório."
                view.SetColumnError(columnDesc, "Campo não pode estar Vazio")
            End If
            Dim columnCode As GridColumn = view.Columns("Code")
            Dim valcode As Object = view.GetRowCellValue(e.RowHandle, columnCode)
            If valcode Is Nothing OrElse valcode.ToString() = String.Empty Then
                e.Valid = False
                e.ErrorText = "Código é obrigatório."
                view.SetColumnError(columnCode, "Campo não pode estar Vazio")
            End If
           
           
            Dim verifica As Integer = -1
            If e.Valid.ToString = True Then
                Dim verificar = (From c In db.tblProjectos _
                              Where c.ano = ComboBoxEdit1.Text AndAlso c.Code = GridView1.GetFocusedRowCellValue(colCode).ToString _
                              Select c.Code).Count
                verifica = verificar
            End If
            If e.Valid.ToString = True And verifica = 0 Then
                TblProjectosTableAdapter.Insert(ComboBoxEdit1.Text, GridView1.GetFocusedRowCellValue(colCode).ToString, GridView1.GetFocusedRowCellValue(colDescricao).ToString, GlobalVariables.Empresa, GridView1.GetFocusedRowCellValue(colun).ToString)
            ElseIf e.Valid.ToString = True And verifica > 0 Then
                e.Valid = False
                e.ErrorText = "Linha já existe"
                view.SetColumnError(colCode, "Linha já existe.")

            End If

        End If
    End Sub

    Private Sub GridControl1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles GridControl1.PreviewKeyDown
        Dim linha As Integer = GridView1.FocusedRowHandle
        If e.KeyCode = Keys.Delete Then
            MessageBox.Show("Deseja eleminar o projecto " & GridView1.GetFocusedRowCellValue(colDescricao) & "?", "Deseja eliminar a linha?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If DialogResult.Yes Then

                Dim DeleteLinha = (From c In db.tblProjectos _
                                   Where c.Code = GridView1.GetFocusedRowCellValue(colCode).ToString _
                                   Select c).SingleOrDefault
                db.tblProjectos.DeleteOnSubmit(DeleteLinha)
                db.SubmitChanges()
                GridView1.DeleteRow(GridView1.FocusedRowHandle)

            End If
        End If
    End Sub
End Class